<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="/template/DefaultTemplate.xhtml">

    <ui:define name="top">
        <title>Announcement</title>
    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="content">
        <f:view contentType="text/html">
            <p:tabView effect="opacity" effectDuration="normal" dynamic="true" cache="false" tabChangeListener="#{announcementController.action}"
                       style="height:1000px" activeIndex="#{announcementController.tabIndex}"> 

                <p:tab title="Create Announcement" id ="createannouncement" rendered="#{loginBacking.createEditDeleteAnnouncement}"> 
                    <h:form>
                        <h1>Create Announcement</h1>
                        <p:growl id="growl" showDetail="true" />
                        <h:panelGrid columns="3">
                            
                            <p:button onclick="selectAll();return;" value="Select All" />
                            <h:outputText value=" " />
                            <h:outputText  value=" " />
                            
                            To: 
                            <h:selectManyListbox id="recipientsunique" value="#{announcementController.selected.departmentRecipients}" 
                                              title="#{bundle.CreateInternalMessageEntityTitle_recipients}" 
                                              converter="departmentConverter" 
                                               style="width:630px">
                                <f:selectItems value="#{departmentEntityController.itemsAvailableSelectMany}"/> 
                                <f:validator validatorId="macTeam.SelectManyListBoxValidator" disabled="#{!empty param['skipvalidation']}"/>
                            </h:selectManyListbox> 
                            <h:message styleClass="errorMsg" for= "recipientsunique" />
                            
                            Subject:
                            <p:inputText id="subject" binding="#{subject}" value="#{announcementController.selected.title}"  style="width:630px" /> 
                            <h:message styleClass="errorMsg" for= "subject" />
                            
                            Message:
                            <p:editor id="message" value="#{announcementController.selected.body}" widgetVar="editor" width="640" height="480" >  
                            <f:validator validatorId="macTeam.InternalMessageValidator" disabled="#{!empty param['skipvalidation']}"/>
                            <f:attribute name="subject" value="#{subject.value}" />
                            </p:editor>
                            <h:message styleClass="errorMsg" for= "message" />
                            
                        </h:panelGrid>
                        <p:commandButton ajax="false" value="Send" actionListener ="#{announcementController.createOrUpdateAnnouncementAndSend}" update="growl">
                        <f:param name="skipvalidation" value="" />
                        </p:commandButton>
                        
                        <p:commandButton ajax="false" value="Save as Draft" actionListener ="#{announcementController.createOrUpdateAnnouncement}" update="growl">
                        <f:param name="skipvalidation" value="1" />
                        </p:commandButton>
                        
                        <p:commandButton ajax="false" value="Discard" actionListener ="#{announcementController.discard}">
                        <f:param name="skipvalidation" value="1" />
                        </p:commandButton>
                        
                    </h:form>
                </p:tab> 

                <p:tab title="Inbox(#{announcementController.totalUnread})" id ="inbox" rendered="#{loginBacking.viewAnnouncement}">
                    <h:form prependId="false">
                        <p:dataTable  value="#{announcementController.msg}" var="message" widgetVar="messageTableInbox"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{announcementController.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{announcementController.messageSelect}" 
                                      rowStyleClass="#{(!message.read) ? 'unread' : null}">
                            
                           <p:column sortBy="#{message.title}" filterBy="#{message.title}"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                               <h:outputText value="#{message.title}" /> </p:column>

                               <p:column sortBy="#{message.sender.name}" filterBy="#{message.sender.name}"> <f:facet name="header">
                                    <h:outputText value="From" /> </f:facet>
                                   <h:outputText value="#{message.sender.name}" /> </p:column>
                                   
                               <p:column sortBy="#{message.departmentRecipientToString}" filterBy="#{message.departmentRecipientToString}"> <f:facet name="header">
                                    <h:outputText value="To" /> </f:facet>
                                   <h:outputText value="#{message.departmentRecipientToString}" /> </p:column>
                            
                            <p:column sortBy="#{message.sentAt.toString()}" filterBy="#{message.sentAt.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Sent" /> </f:facet>
                                <h:outputText value="#{message.sentAt.toString()}" /> </p:column>


                        </p:dataTable> 
                    </h:form>
                </p:tab>

                <p:tab title="Posted" id ="sent" rendered="#{loginBacking.createEditDeleteAnnouncement}">
                    <h:form>
                        <p:dataTable  value="#{announcementController.msg}" var="message" widgetVar="messageTable"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{announcementController.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{announcementController.messageSelect}" >

                            <p:column sortBy="#{message.title}" filterBy="#{message.title}"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                               <h:outputText value="#{message.title}" /> </p:column>

                               <p:column sortBy="#{message.sender.name}" filterBy="#{message.sender.name}" > <f:facet name="header">
                                    <h:outputText value="From" /> </f:facet>
                                   <h:outputText value="#{message.sender.name}" /> </p:column>
                                   
                               <p:column sortBy="#{message.departmentRecipientToString}" filterBy="#{message.departmentRecipientToString}"> <f:facet name="header">
                                    <h:outputText value="To" /> </f:facet>
                                   <h:outputText value="#{message.departmentRecipientToString}" /> </p:column>
                            
                            <p:column sortBy="#{message.sentAt.toString()}" filterBy="#{message.sentAt.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Sent" /> </f:facet>
                                <h:outputText value="#{message.sentAt.toString()}" /> </p:column>

                        </p:dataTable>  
                    </h:form>
                </p:tab>

                <p:tab title="Drafts" id ="drafts" rendered="#{loginBacking.createEditDeleteAnnouncement}">
                    <h:form>
                        <p:dataTable  value="#{announcementController.msg}" var="message" widgetVar="messageTable"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      >        

                            <p:column sortBy="#{message.title}" filterBy="#{message.title}"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                               <h:outputText value="#{message.title}" /> </p:column>

                               <p:column sortBy="#{message.sender.name}" filterBy="#{message.sender.name}"> <f:facet name="header">
                                    <h:outputText value="From" /> </f:facet>
                                   <h:outputText value="#{message.sender.name}" /> </p:column>
                                   
                               <p:column sortBy="#{message.departmentRecipientToString}" filterBy="#{message.departmentRecipientToString}"> <f:facet name="header">
                                    <h:outputText value="To" /> </f:facet>
                                   <h:outputText value="#{message.departmentRecipientToString}" /> </p:column>
                            
                            <p:column sortBy="#{message.sentAt.toString()}" filterBy="#{message.sentAt.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Sent" /> </f:facet>
                                <h:outputText value="#{message.sentAt.toString()}" /> </p:column>

                            <p:column>
                                <p:commandButton value="Edit" ajax="false" action="#{announcementController.draftSelect}">
                                    <f:setPropertyActionListener value="#{message}"  
                                                                 target="#{announcementController.selectedMessage}" /> 
                                </p:commandButton>
                            </p:column>
                            
                            <p:column style="width:32px">  
                                <p:commandButton value="Delete" ajax="false" actionListener="#{announcementController.deleteMessage}" >  
                                    <f:setPropertyActionListener value="#{message}" target="#{announcementController.messageTobeDeleted}" />  
                                </p:commandButton>  
                            </p:column>

                        </p:dataTable>   
                    </h:form>
                </p:tab>
            </p:tabView>
        </f:view>
        
        <script language="JavaScript" type="text/javascript">
/* <![CDATA[ */
function selectAll()
{ 
  jQuery(document).find('[title="Recipients"]').each(function(index){
      var i;
      for(i=0;i<this.options.length;i++){
        this.options[i].selected=true;
      }
  });
}
/* ]]> */
</script>
    </ui:define>

</ui:composition>
